//
//  AppDelegate.m
//  news
//
//  Created by imacbookair on 16/3/7.
//  Copyright © 2016年 imacbookair. All rights reserved.
//

#import "AppDelegate.h"
#import "BaseViewController.h"
#import "JPUSHService.h"
#import "NewsViewController.h"
#import "MineViewController.h"
#import "TopicViewController.h"
#import "VideoViewController.h"
#import "ReadViewController.h"
#import "LeadPageViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
#pragma mark - 数据库
    // 创建单例对象
    DataBaseManager *dbManger = [DataBaseManager shareInstance];
    // 打开数据库
    [dbManger open];
    // 创建表
    [[DataBaseManager shareInstance]create];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor grayColor];
    [self.window makeKeyAndVisible];
    
//    BaseViewController *bVC = [[BaseViewController alloc]init];
//    bVC.view.backgroundColor = [UIColor lightGrayColor];
    
    // 夜间模式通知中心
    [ThemeManage shareThemeManage].isNight = [[NSUserDefaults standardUserDefaults]boolForKey:@"night"];
    // 引导页
    LeadPageViewController *lPVC = [[LeadPageViewController alloc]init];
    
    
//    [self.window setRootViewController:lPVC];

    self.window.rootViewController = lPVC;
    
    
    // 创建VC/*News**/
    NewsViewController *root = [[NewsViewController alloc]init];
    // 创建导航
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:root];

    // 创建标签控制器
//    UITabBarController *taBar = [[UITabBarController alloc]init];
//    navi.navigationController.navigationBar.translucent = NO;
    // 设置tabBar 按钮
    root.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"新闻" image:[UIImage imageNamed:@"xinwen"] tag:1000];
    
    /** read **/
    ReadViewController *rootOne = [[ReadViewController alloc]init];
    UINavigationController *naviOne = [[UINavigationController alloc]initWithRootViewController:rootOne];
    rootOne.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"阅读" image:[UIImage imageNamed:@"yuedu"] tag:2000];
    /** video **/
    VideoViewController *rootTow = [[VideoViewController alloc]init];
    UINavigationController *naviTow = [[UINavigationController alloc]initWithRootViewController:rootTow];
    rootTow.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"视频" image:[UIImage imageNamed:@"shipin"] tag:3000];
    /** topic **/
    TopicViewController *rootThree = [[TopicViewController alloc]init];
    UINavigationController *naviThree = [[UINavigationController alloc]initWithRootViewController:rootThree];
    rootThree.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"话题" image:[UIImage imageNamed:@"huati"] tag:4000];
    /** mine   **/
    MineViewController *rootFour = [[MineViewController alloc]init];
    UINavigationController *naviFour = [[UINavigationController alloc]initWithRootViewController:rootFour];
    rootFour.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[UIImage imageNamed:@"wode"] tag:5000];
    
    
    [navi.navigationBar NightWithType:UIViewColorType1];
    [naviOne.navigationBar NightWithType:UIViewColorType1];
    [naviTow.navigationBar NightWithType:UIViewColorType1];
    [naviThree.navigationBar NightWithType:UIViewColorType1];
    [naviFour.navigationBar NightWithType:UIViewColorType1];
    
    // 添加管理控制器
    
    self.tabBar = [[UITabBarController alloc]init];
    
    _tabBar.viewControllers = @[navi, naviOne, naviTow, naviThree, naviFour];
    
    [_tabBar.tabBar NightWithType:UIViewColorType1];
    // 设置window的根视图
//    self.window.rootViewController = taBar;
    
//    self.tabBar = taBar;
    
    
#pragma mark - 极光推送
    // Required
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        // 可以添加自定义categories
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil];
    } else {
        // catefories 必须加nil
        [JPUSHService registerForRemoteNotificationTypes:( UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound) categories:nil];
    }
    
    // Required
    // 如需兼容旧版本的方式, 请依旧使用[JPUSHService setupWithOption: launchOptions]方式初始化和同时使用pushConfig.plist文件声明appKey等配置内容
//    [JPUSHService setupWithOption:launchOptions appKey:appKey channel:channel apsForProduction:isProduction];
    /// 配置内容
    [JPUSHService setupWithOption:launchOptions appKey:@"8d7a6cc3921c3cdec626f8c6" channel:nil apsForProduction:NO];
    
      
    
    
    
    
    
    return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    // Required
    [JPUSHService registerDeviceToken:deviceToken];
//    NSLog(@"token:%@", deviceToken);
    
    
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    // Required, For system with less than or equal to iOS6
    [JPUSHService handleRemoteNotification:userInfo];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    // IOS 7 Support Required
    
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    // Optional
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}



@end
